Ext.define('Dashboard.AppConstants', {
	alias : 'widget.AppConstants',
	config : {
		paramMap : {},
		lockTaskMainInfo : false,
		// 任务名称tip展示字段
		quikTipFields : [ 'taskName', 'status', 'progress', 'remark', 'createTime' ],
		// 隐藏的字段
		hiddenFileds : [ 'taskId', 'workDays', 'month', 'realStartDate', 'realEndDate', 'realOnlieDate', 'updateTime', 'userId', 'userName', 'updateUserId', 'updateUsername', 'isDeleted' ],
		//参数类型(1.参数类型,2.任务状态,3.任务类型,4.月度范围,5.友情链接,6.滚动tip,7.优先级,8.jira前缀,9.项目列表,10.开发成员,11.测试成员)
		paramType : 1,
		taskStatus : 2,
		taskType : 3,
		monthRange : 4,
		friendLink : 5,
		scrollTip : 6,
		prioritiyLevel : 7,
		jiraPrefix : 8,
		application : 9,
		developer : 10,
		tester : 11,
	},
	constructor : function(options) {
		let me = this;
		Ext.Ajax.request({
			url : 'param/load',
			async : false,
			params : {
			},
			success : function(response) {
				let text = response.responseText;
				let responseJson = Ext.util.JSON.decode(response.responseText);
				console.log('init AppConstants:%o', responseJson);
				let paramTypeMap = responseJson.data;
				me.initConfig({
					paramMap : paramTypeMap,
					//hiddenFileds : []
				});
			}
		});
	},
	getParamValue : function(paramTypeName) {
		let data = this.loadParamByName(paramTypeName);
		if (data && data.length > 0) {
			let param = data[0];
			return param.value;
		}
		return [];
	},
	getTypeByName : function(paramTypeName) {
		let me = this;
		let config = me.getConfig();
		if (!config) {
			return;
		}
		let paramType = config[paramTypeName];
		return paramType;
	},
	loadParamByName : function(paramTypeName) {
		// 参数类型(1.参数类型,2.任务状态,3.任务类型,4.月度范围,5.友情链接,6.滚动tip,7.优先级,8.jira前缀,9.项目列表,10.开发成员,11.测试成员)
		let paramType = this.getTypeByName(paramTypeName);
		if (!paramType) {
			return {};
		}
		return this.loadParam(paramType);
	},
	loadParam : function(paramType) {
		let me = this;
		if (me.getMonthRange() != paramType) {
			let params = me.getParamMap()[paramType];
			return params || [];
		}
		// 月度范围配置格式:201812-now 或 201812-201911
		let monthData = [ {
			value : '全选'
		} ];
		let params = me.getParamMap()[paramType];
		if (params && params.length > 0) {
			let value = params[0];
			let valArr = value.value.split('-');
			let start = valArr[0];
			let end = 'now';
			if (valArr.length > 1) {
				end = valArr[1];
			}
			let startDate = Ext.Date.parseDate(start, 'Ym');
			let endDate;
			if ('now' == end) {
				endDate = new Date();
			} else {
				endDate = Ext.Date.parseDate(end, 'Ym');
			}
			while (startDate * 1 < endDate * 1) {
				monthData.push({
					value : Ext.Date.dateFormat(endDate, 'Ym')
				});
				endDate = Ext.Date.add(endDate, Ext.Date.MONTH, -1)
			}
		}
		return monthData;
	},
	loadParamMap : function(paramTypeName) {
		// 参数类型(1.参数类型,2.任务状态,3.任务类型,4.月度范围,5.友情链接,6.滚动tip,7.优先级,8.jira前缀,9.项目列表,10.开发成员,11.测试成员)
		let paramType = this.getTypeByName(paramTypeName);
		if (!paramType) {
			return {};
		}
		let me = this;
		let params = me.getParamMap()[paramType];
		let map = {};
		for (let i in params) {
			let p = params[i];
			map[p.value] = p.name;
		}
		return map;
	},
	loadFriendLink : function() {
		let linkparam = this.loadParam(this.getFriendLink());
		let tpl = new Ext.XTemplate(
			'<tpl for=".">',
			' <a class="panel-link" href="{value}" target="_blank"> {name}</a> ',
			'</tpl>',
		);
		let friendLinkHtml = tpl.apply(linkparam);
		return friendLinkHtml;
	}
});